package cc.comac.mom.datahandlers.entity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import cc.comac.mom.bean.EquipMaintainWorkOrder;
import cc.comac.mom.dao.entity.*;
import cc.comac.mom.entity.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipMaintainSpareReplace;
import cc.comac.mom.bean.EquipSparePartsDue;
import cc.comac.mom.bean.base.LimitPageBean;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;
import cc.comac.mom.util.TimeUtil;

@Service
@Transactional
public class EquipMaintainSpareReplaceDataHandler
		extends ServiceImpl<EquipMaintainSpareReplaceDao, EquipMaintainSpareReplaceEntity> {

	@Autowired
	private EquipMaintainSpareReplaceDao equipMaintainSpareReplaceDao;

	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	@Autowired
	private EquipPartBorrowDemandDataHandler equipPartBorrowDemandDataHandler;
	
	@Autowired
	private EquipSparePartMasterDataDao equipSparePartMasterDataDao;
	
	@Autowired
	private EquipSparePartsDueDataHandler equipSparePartsDueDataHandler;
	@Autowired
	private EquipMaintainWorkOrderDao equipMaintainWorkOrderDao;
	
	@Autowired
	private EquipDevAreaDefDao equipDevAreaDefDao;


	// 批量逻辑删除对应数据
	public ResultBean deleteSpareReplace(List<EquipMaintainSpareReplace> list) {
		ResultBean resultBean = new ResultBean(true);
		if (list.size() > 0) {
			List<Long> ids = new ArrayList<Long>();
			for (EquipMaintainSpareReplace equipMaintainSpareReplace : list) {
				ids.add(equipMaintainSpareReplace.getInnerData().getId());
			}
			if (ids.size() > 0) {
				this.removeByIds(ids);
			}
		}
		return resultBean;
	}
	// 更新备件替换表
	public ResultBean updateSpareReplace(List<EquipMaintainSpareReplace> spareReplaceList) {
		ResultBean resultBean = new ResultBean(true);
		if (spareReplaceList.size() > 0) {
			List<EquipMaintainSpareReplaceEntity> updateEntityList = new ArrayList<EquipMaintainSpareReplaceEntity>();
			for (EquipMaintainSpareReplace equipMaintainSpareReplace : spareReplaceList) {
				EquipMaintainSpareReplaceEntity innerData = equipMaintainSpareReplace.getInnerData();
				innerData.setReplaceStatus("0");
				if (innerData.getReplaceQuantity().compareTo(new BigDecimal(0))==0) {
					throw new RuntimeException("数量不能为空！");
				}
				if (StringUtils.isEmpty(innerData.getReplacePart())) {
					throw new RuntimeException("部位不能为空！");
				}
				if (innerData.getSparePartId() == 0) {
					throw new RuntimeException("备件不能为空！");
				}
				baseEntityDataUtil.updateBaseEntityData(innerData);
				updateEntityList.add(innerData);

			}
			// 修改
			if (updateEntityList.size() > 0) {
				this.updateBatchById(updateEntityList, updateEntityList.size());
			}
		}
		return resultBean;
	}
	// 查询备件替换表
	public ResultBean selectSomeByFields(EquipMaintainSpareReplace equipMaintainSpareReplace) {
		ResultBean resultBean = new ResultBean(true);
		EquipMaintainSpareReplaceEntity innerData = equipMaintainSpareReplace.getInnerData();
		if (equipMaintainSpareReplace.getPageNum()>0) {
			equipMaintainSpareReplace.setPageNum(1);
		}
		if (equipMaintainSpareReplace.getPageSize()> 0) {
			equipMaintainSpareReplace.setPageSize(10);
		}
		Page<EquipMaintainSpareReplaceEntity> page = new Page<EquipMaintainSpareReplaceEntity>(equipMaintainSpareReplace.getPageNum(),equipMaintainSpareReplace.getPageSize());
		QueryWrapper<EquipMaintainSpareReplaceEntity> queryWrapper = new QueryWrapper<EquipMaintainSpareReplaceEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("order_number", innerData.getOrderNumber());
		//queryWrapper.ne("replace_status", "2");
		IPage<EquipMaintainSpareReplaceEntity> selectPage = equipMaintainSpareReplaceDao.selectPage(page, queryWrapper);
		LimitPageBean<EquipMaintainSpareReplaceEntity> of = (LimitPageBean<EquipMaintainSpareReplaceEntity>) LimitPageBean.of(selectPage);
		resultBean.setObject(of);
		return resultBean;
	}

	// 保存按钮
	public ResultBean saveSpareReplace(List<EquipMaintainSpareReplace> spareReplaceList) {
		ResultBean resultBean = new ResultBean(true);
		if (spareReplaceList.size() > 0) {
			List<EquipMaintainSpareReplaceEntity> addEntityList = new ArrayList<EquipMaintainSpareReplaceEntity>();
			for (EquipMaintainSpareReplace equipMaintainSpareReplace : spareReplaceList) {

				// 查询对应维修数据
				QueryWrapper<EquipMaintainWorkOrderEntity> queryWrapper2 = new QueryWrapper<EquipMaintainWorkOrderEntity>();
				queryWrapper2.eq("row_status", 1);
				queryWrapper2.eq("order_number", equipMaintainSpareReplace.getInnerData().getOrderNumber());
				EquipMaintainWorkOrderEntity entity = equipMaintainWorkOrderDao.selectOne(queryWrapper2);

				EquipMaintainSpareReplaceEntity innerData = equipMaintainSpareReplace.getInnerData();
				innerData.setDevName(entity.getDevName());
				innerData.setDevNumber(entity.getDevNumber());
				innerData.setUserDepartment(entity.getUserDepartment());
				innerData.setReplaceStatus("0");
				if (innerData.getReplaceQuantity().compareTo(new BigDecimal(0))==0) {
					throw new RuntimeException("数量不能为空！");
				}
				if (StringUtils.isEmpty(innerData.getReplacePart())) {
					throw new RuntimeException("部位不能为空！");
				}
				if (innerData.getSparePartId() == 0) {
					throw new RuntimeException("备件不能为空！");
				}
				baseEntityDataUtil.setBaseEntityData(innerData);
				addEntityList.add(innerData);

			}
			// 新增
			if (addEntityList.size() > 0) {
				this.saveBatch(addEntityList, addEntityList.size());
			}
		}
		return resultBean;
	}

	// 批量更换确认
	public ResultBean batchReplaceConfirm(List<EquipMaintainSpareReplace> spareReplaceList) {
		ResultBean resultBean = new ResultBean(true);
		// 设置更换时间及更新更新时间
		if (spareReplaceList.size() > 0) {
			List<Long> idList = new ArrayList<Long>();
			for (EquipMaintainSpareReplace equipMaintainSpareReplace : spareReplaceList) {
				idList.add(equipMaintainSpareReplace.getInnerData().getId());
			}
			if (idList.size() > 0) {
				List<EquipMaintainSpareReplaceEntity> selectBatchIds = equipMaintainSpareReplaceDao.selectBatchIds(idList);
				List<EquipSparePartsDue> sparePartsDueList = new ArrayList<EquipSparePartsDue>();
				if (selectBatchIds.size() > 0) {
					for (EquipMaintainSpareReplaceEntity equipMaintainSpareReplaceEntity : selectBatchIds) {
						equipMaintainSpareReplaceEntity.setReplaceTime(TimeUtil.getLocalDate());
						equipMaintainSpareReplaceEntity.setReplaceStatus("1");
						baseEntityDataUtil.updateBaseEntityData(equipMaintainSpareReplaceEntity);
						
						
						// 查询对应备件数据
						QueryWrapper<EquipSparePartMasterDataEntity> queryWrapper = new QueryWrapper<EquipSparePartMasterDataEntity>();
						queryWrapper.eq("row_status", 1);
						queryWrapper.eq("spare_part_id", equipMaintainSpareReplaceEntity.getSparePartId());
						EquipSparePartMasterDataEntity selectOne = equipSparePartMasterDataDao.selectOne(queryWrapper);
						// 新增到期领用数据
						EquipSparePartsDue equipSparePartsDue = new EquipSparePartsDue();
						EquipSparePartsDueEntity equipSparePartsDueEntity = new EquipSparePartsDueEntity();
						equipSparePartsDue.setInnerData(equipSparePartsDueEntity);
						EquipDevAreaDefEntity selectById = equipDevAreaDefDao.selectById(equipMaintainSpareReplaceEntity.getReplacePart());
						equipSparePartsDueEntity.setBenchmarkDevPart(selectById.getAreaNumber());
						equipSparePartsDueEntity.setDevName(equipMaintainSpareReplaceEntity.getDevName());
						equipSparePartsDueEntity.setDevNumber(equipMaintainSpareReplaceEntity.getDevNumber());
						equipSparePartsDueEntity.setMaterialType(equipMaintainSpareReplaceEntity.getSparePartType());
//						equipSparePartsDueEntity.setPartName(equipMaintainSpareReplaceEntity.getSparePartName());
						equipSparePartsDueEntity.setPartNumber(equipMaintainSpareReplaceEntity.getSparePartNumber());
						equipSparePartsDueEntity.setReplacementNumber(equipMaintainSpareReplaceEntity.getReplaceQuantity().intValue());
						equipSparePartsDueEntity.setReplacementTime(TimeUtil.getLocalDateTime());
						equipSparePartsDueEntity.setReplacementType("2");
						equipSparePartsDueEntity.setSpecifications(selectOne.getSparePartModel());
//						equipSparePartsDueEntity.setUnit(selectOne.getSparePartsUnit());
						equipSparePartsDueEntity.setWorkNumber(equipMaintainSpareReplaceEntity.getOrderNumber());
						sparePartsDueList.add(equipSparePartsDue);
					}
					// 更新保养更换备件数据
					this.updateBatchById(selectBatchIds, selectBatchIds.size());
					// 批量新增备件到期数据
					equipSparePartsDueDataHandler.save(sparePartsDueList);
				}
			}
		}
		return resultBean;
	}

	// 批量需求确认
	public ResultBean batchDemandConfirm (List<EquipMaintainSpareReplace> beanlist) {
		ResultBean resultBean = new ResultBean(true);
		if (beanlist.size() > 0) {
			// 用于存领用需求数据
			List<EquipPartBorrowDemandEntity> borrowDemand = new ArrayList<EquipPartBorrowDemandEntity>();
			List<Long> ids = new ArrayList<Long>();
			for (EquipMaintainSpareReplace equipMaintainSpareReplace : beanlist) {
				// 备件替换数据
				ids.add(equipMaintainSpareReplace.getInnerData().getId());
				EquipMaintainSpareReplaceEntity innerData = equipMaintainSpareReplace.getInnerData();
				// 将数据更新到备件领用的领用需求表
				EquipPartBorrowDemandEntity equipPartBorrowDemandEntity = new EquipPartBorrowDemandEntity();
				equipPartBorrowDemandEntity.setApplyQuantity(innerData.getReplaceQuantity());
				equipPartBorrowDemandEntity.setApplyTime(TimeUtil.getLocalDate());
				equipPartBorrowDemandEntity.setApplyUser(baseEntityDataUtil.getCurrentUser());
				equipPartBorrowDemandEntity.setBorrowDemandStatus("1");
				equipPartBorrowDemandEntity.setBorrowStyle("2");
				equipPartBorrowDemandEntity.setDevName(innerData.getDevName());
				equipPartBorrowDemandEntity.setDevNumber(innerData.getDevNumber());
				equipPartBorrowDemandEntity.setRelationNumber(innerData.getOrderNumber());
				equipPartBorrowDemandEntity.setSparePartId(innerData.getSparePartId());
				equipPartBorrowDemandEntity.setSparePartModel(innerData.getSparePartModel());
				equipPartBorrowDemandEntity.setJobNumber(baseEntityDataUtil.getJobNumber());
				equipPartBorrowDemandEntity.setSparePartName(innerData.getSparePartName());
				equipPartBorrowDemandEntity.setSparePartNumber(innerData.getSparePartNumber());
				equipPartBorrowDemandEntity.setUserDepartment(innerData.getUserDepartment());
				baseEntityDataUtil.setBaseEntityData(equipPartBorrowDemandEntity);
				borrowDemand.add(equipPartBorrowDemandEntity);
				
				
			}
			// 批量新增领用需求数据
			equipPartBorrowDemandDataHandler.saveBatch(borrowDemand, borrowDemand.size());

			// 批量查询维修备件更换
			List<EquipMaintainSpareReplaceEntity> selectBatchIds = equipMaintainSpareReplaceDao.selectBatchIds(ids);
			for (EquipMaintainSpareReplaceEntity e : selectBatchIds) {
				e.setSubmitTime(TimeUtil.getLocalDate());
				e.setSubmitReceive(1);
			}
			this.updateBatchById(selectBatchIds, selectBatchIds.size());
		}
		
		return resultBean;
	}
	
	
}
